package com.jingyanku.service.impl;

import com.base.service.impl.ServiceImpl;
import com.jingyanku.pojo.Reply;
import com.jingyanku.service.IReplyService;

public class ReplyServiceImpl<T extends Reply> extends ServiceImpl<T> implements
		IReplyService<T> {

	@Override
	public boolean create(T reply) {

		dao.create(reply);

		// 更新帖子最后回复、最后回复日期、作者、回帖数
		int count = dao.getTotalCount(" select count(r) from Reply r "
				+ " where r.deleted = false and r.thread.id = "
				+ reply.getThread().getId(), null);
		String sql =null;

		dao.createQuery(" update Thread t  set t.authorLastReplied.id = "
				+ reply.getAuthor().getId() 
				+ ", t.dateLastReplied = '" + reply.getDateCreated()
				+ "', t.replyCount = " + count + " where t.id ="
				+ reply.getThread().getId()).executeUpdate();

		// 更新版面的最后发表数、最后发表时间
		int replyCount = dao.getTotalCount(" select count(r) from Reply r "
				+ " where r.deleted = false " + " and r.thread.board.id = "
				+ reply.getThread().getBoard().getId(), null);

		dao.createQuery(
				" update Board b  set b.lastThread.id = null,  b.lastReply.id = " + reply.getId() + ", b.replyCount = " + replyCount + " where b.id="
						+ reply.getThread().getBoard().getId()).executeUpdate();

		int floor = dao.getTotalCount(" select count(r) from Reply r "
				+ " where r.thread.id = " + reply.getThread().getId(), null);

		// 回帖处于第几楼
		reply.setFloor(floor);

		dao.save(reply);
		return true;
	}
}
